package com.wh.service.outstorehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.api.Api;
import com.wh.mapper.initial.ProductMapper;
import com.wh.mapper.outstorehouse.OutStorehouseMapper;
import com.wh.mapper.outstorehouse.PickingMapper;
import com.wh.mapper.stock.StockMapper;
import com.wh.pojo.*;
import com.wh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class OutStorehouseServiceImpl implements OutStorehouseService {

    @Autowired
    private OutStorehouseMapper outStorehouseMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PickingMapper pickingMapper;

    @Override
    public Map<String, Object> getQuery(OutStorehouse outStorehouse) {
        Map<String,Object> map = new HashMap<>();
        Set<String> barCodeList = new HashSet<>();
        Set<String> locationIdList = new HashSet<>();
        String productName = "";
        Integer availableQuantity = 0;


        boolean barCodeFlag = StringUtils.hasLength(outStorehouse.getBarCode());
        boolean locationIdFlag = StringUtils.hasLength(outStorehouse.getLocationId());

        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(barCodeFlag,"bar_code",outStorehouse.getBarCode())
                .eq(locationIdFlag,"location_id",outStorehouse.getLocationId())
                .eq("status","已上架");

        List<Stock> stockList = stockMapper.selectList(queryWrapper);

        if (stockList.size() == 1){
            productName = stockList.get(0).getProductName();
            availableQuantity = stockList.get(0).getAvailableQuantity();

        }

        for (Stock stock : stockList){
            barCodeList.add(stock.getBarCode());
            locationIdList.add(stock.getLocationId());
        }

        map.put("barCodeList",barCodeList);
        map.put("locationIdList",locationIdList);
        map.put("availableQuantity",availableQuantity);
        map.put("productName",productName);


        return map;
    }

    @Override
    public PageResult getOutStorehouseList(PageResult pageResult) {
        Page<OutStorehouse> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<OutStorehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag,"out_storehouse_id",pageResult.getQuery());
        page = outStorehouseMapper.selectPage(page,queryWrapper);
        pageResult.setTotal((long) page.getRecords().size()).setRows(page.getRecords());
        return pageResult;
    }

    @Override
    public void addOutStorehouse(OutStorehouse outStorehouse) {
        outStorehouse.setCreateTime(new Api().getTime());
        /**修改stock*/
        Stock stock = stockMapper.selectById(outStorehouse.getLocationId());
        stock.setAvailableQuantity(
                stock.getAvailableQuantity() - outStorehouse.getCount()
        );
        stockMapper.updateById(stock);
        outStorehouseMapper.insert(outStorehouse);
    }

    @Override
    public void updataOutStorehouse(OutStorehouse outStorehouse) {
        /** 修改stock*/
        Stock stock = stockMapper.selectById(outStorehouse.getLocationId());
        if (outStorehouse.getStatus().equals("已审核")){
            //设置锁定数量
            stock.setLockedQuantity(
                    stock.getLockedQuantity() + outStorehouse.getCount()
            );
            //设置审核时间
            outStorehouse.setCheckTime(new Api().getTime());
            //设置拣货id/添加拣货单
            if (!StringUtils.hasLength(outStorehouse.getPickingId())){
                String pickingId = "PI" + System.currentTimeMillis();
                outStorehouse.setPickingId(pickingId);
                Picking picking = new Picking();
                picking.setPickingId(pickingId)
                        .setOutStorehouseId(outStorehouse.getOutStorehouseId())
                        .setCreatePerson(outStorehouse.getCheckPerson())
                        .setCreateTime(new Api().getTime())
                        .setStatus("待拣货");
                pickingMapper.insert(picking);
            }
        } else if (outStorehouse.getStatus().equals("待审核")){
            stock.setLockedQuantity(
                    stock.getLockedQuantity() - outStorehouse.getCount()
            );
            if (StringUtils.hasLength(outStorehouse.getPickingId())){
                pickingMapper.deleteById(outStorehouse.getPickingId());
            }
            outStorehouse.setPickingId("")
                    .setCheckPerson("")
                    .setCheckTime("");
        }
        stockMapper.updateById(stock);
        outStorehouseMapper.updateById(outStorehouse);
    }

    @Override
    public void deleteOutStorehouse(String id , Integer count , String locationId) {

        Stock stock = stockMapper.selectById(locationId);
        stock.setAvailableQuantity(
                stock.getAvailableQuantity() + count
        );
        stockMapper.updateById(stock);

        outStorehouseMapper.deleteById(id);
    }

    @Override
    public void out(OutStorehouse outStorehouse) {
        outStorehouse.setOutTime(new Api().getTime());
        outStorehouseMapper.updateById(outStorehouse);

        Stock stock = stockMapper.selectById(outStorehouse.getLocationId());
        stock.setTotalQuantity(stock.getTotalQuantity() - outStorehouse.getCount())
                .setLockedQuantity(stock.getLockedQuantity() - outStorehouse.getCount());
        stockMapper.updateById(stock);
    }


}
